import bisect

def calc_row_index_list(s):
    """
    Returns a list of integers.
    Each integer means an index of first character of each line 
    in the argument string "s".
    """
    lines = s.splitlines(True)
    
    rowIndices = list(); rowIndices_append = rowIndices.append
    i = 0
    for L in lines:
        rowIndices_append(i)
        i += (1 if not L else \
                len(L) if L[-1] in ('\r', '\n') else \
                len(L) + 1)
    else:
        rowIndices_append(i)
    return rowIndices

def row_index_list_to_row_col_table(rowIndices, s):
    """
    Converts a row indices to row-col table.
    The argument "s" is the original string, which is 
    used to generate the "rowIndices".
    """
    rowcolTable = list()
    row = 1
    for i, j in zip(rowIndices, rowIndices[1:]):
        rowcolTable.extend((row, col) for col in xrange(1, j - i + 1))
        row += 1
    if len(rowcolTable) < len(s) + 1:
        rowcolTable.append((row, 1))
    return rowcolTable

def get_row_col_at(rowIndices, index):
    """
    Calculate row and col from a row indices and an index in a string.
    """
    b = bisect.bisect_right(rowIndices, index)
    if b < len(rowIndices):
        return b, index - rowIndices[b - 1] + 1
    else:
        return b, index - rowIndices[-1] + 1

def get_row_at(rowIndices, index):
    """
    Calculate row from a row indices and an index in a string.
    """
    return bisect.bisect_right(rowIndices, index)

#if __name__ == '__main__':
#    import sys
#
#    s1 = "abc\n,def\n,ghi\n".split(",")
#    s2 = "abc\n,def\n,ghi".split(",")
#    s3 = "abc\n,def\n,\n".split(",")
#    s4 = "abc\n,\n,\n,def\n".split(",")
#    s5 = "\n,\n".split(",")
#    s6 = "\n"
#    s7 = ""
#    ss = ["".join(s) for s in [ s1, s2, s3, s4, s5, s6, s7 ]]
#    
#    for sIndex, s in enumerate(ss):
#        tbl1 = calc_row_col_table(s)
#        lst4 = calc_row_index_list(s)
#        
#        for i in xrange(len(s) + 1):
#          rowCol1 = tbl1[i]
#          rowCol4 = get_row_col_at(lst4, i)
#          if rowCol1 != rowCol4:
#            print "differs (1)!", i, rowCol1, rowCol4
#            assert False
#        
#        recovTable = row_index_list_to_row_col_table(lst4, s)
#        assert len(recovTable) == len(tbl1)
#        for i in xrange(len(tbl1)):
#          if tbl1[i] != recovTable[i]:
#            print "differs (2)!", i, tbl1[1], recovTable[i]
#            assert False
#    
#    print "ok!"
    
if __name__ == '__main__':
    import sys
    import time
    
    T = 100
    
    f = sys.argv[1]
    s = file(f).read()
    
    t1 = time.time()
    for t in xrange(T):
        calc_row_index_list(s)
    t2 = time.time()
    
    print t2 - t1

